package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.ChargeProjectAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ChargeProjectUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.AssetEquipment;
import tk.uodrad.apex.project.pojo.entity.ChargeDetails;
import tk.uodrad.apex.project.pojo.entity.ChargeProject;
import tk.uodrad.apex.project.pojo.vo.ChargeDetailsListVO;
import tk.uodrad.apex.project.pojo.vo.ChargeProjectListVO;
import tk.uodrad.apex.project.service.ChargeProjectService;
import tk.uodrad.apex.project.mapper.ChargeProjectMapper;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.utils.InitRandomCode;
import tk.uodrad.apex.project.web.ServiceCode;

/**
* @author Administrator
* @description 针对表【charge_project_management】的数据库操作Service实现
* @createDate 2023-04-06 19:36:08
*/
@Slf4j
@Service
public class ChargeProjectServiceImpl extends ServiceImpl<ChargeProjectMapper, ChargeProject>
    implements ChargeProjectService{

    @Autowired
    private ChargeProjectMapper chargeProjectMapper;

    @Override
    public void save(ChargeProjectAddNewDTO chargeProjectAddNewDTO) {
//新建一个chargeProject对象并将传入的DTO复制给chargeProject
        ChargeProject chargeProject = new ChargeProject();
        BeanUtils.copyProperties(chargeProjectAddNewDTO, chargeProject);
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<ChargeProject> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有id等于当前chargeProject的id
        wrapper.eq("code", chargeProject.getCode());
        ChargeProject selectOne = chargeProjectMapper.selectOne(wrapper);
        if (selectOne != null) {
            String message = "新增数据失败，当前数据已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //生成编号
        chargeProject.setCode(InitRandomCode.initCode("cpm"));
        //插入数据，并判断是否插入成功
        int rows = chargeProjectMapper.insert(chargeProject);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public ChargeProject selectById(Long id) {
        ChargeProject queryResult=chargeProjectMapper.selectById(id);
        if (queryResult == null) {
            String message = "查询收费项目详情失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public void updateById(ChargeProjectUpdateDTO chargeProjectUpdateDTO) {
//根据传入的DTO中的ID查询数据库中是否存在该数据
        ChargeProject chargeProjectById = chargeProjectMapper.selectById(chargeProjectUpdateDTO.getId());
        if (chargeProjectById == null) {
            String message = "修改数据失败，当前数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //创建一个chargeProject对象，将传入的DTO复制给chargeProject对象
        ChargeProject chargeProject = new ChargeProject();
        BeanUtils.copyProperties(chargeProjectUpdateDTO, chargeProject);
        //更新数据，并判断是否更新成功
        int rows = chargeProjectMapper.updateById(chargeProject);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
    /**
     * 分页查询收费项目列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<ChargeProjectListVO> ChargeProjectListVO分页对象
     */
    @Override
    public IPage<ChargeProjectListVO> list(Integer pageNum, Integer pageSize, String communityCode) {
        Page<ChargeProjectListVO> page = new Page<>(pageNum - 1, pageSize);
        return chargeProjectMapper.selectPageVo(page, communityCode);
    }
}




